const { toUrl, globalData } = getApp();
import { request } from "@/utils/request";
Page({
  toUrl,
  data: {
    globalData,
    order_id: 0,
    orderInfoData: {},
    admins: 0, //0 隐藏 1 显示
    writeoff: 0, //是否核销
    times1:null
  },
  //拨打电话
  makePhoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.orderInfoData.store.suppliers_phone, //仅为示例，并非真实的电话号码
    });
  },
  getDetail() {
    let data = {
      order_id: this.data.order_id,
    };
    request("order/orderinfo", { data }).then((res) => {
      if (res.errcode == 1) {
        let contents
            let zhengze = new RegExp('<img', 'g')
						let zhengze1 = new RegExp('<p', 'g')
            contents = res.data.purchase_notes.replace(zhengze1, '<p style="width:100%;height:auto;display:block;margin:10px 0;" ')
            contents = contents.replace(zhengze, '<img style="width:100%;height:auto;display:block;" ')
            res.data.purchase_notes = contents

        this.setData({
          orderInfoData: res.data,
        });

        
      }
    });
  },

  //核销订单
  writeoff() {
    let data = {
      order_id: this.data.order_id,
      // writeoff_code: 202305410816,
    };
    request("order/writeoff", { data }).then((res) => {
      if (res.errcode == 1) {
        this.setData({
          writeoff: 1,
        });
        wx.showToast({
          title: "核销成功",
        });
      }
    });
  },
  goBack() {
    let pages = getCurrentPages();
    if (pages[pages.length - 2]) {
      //如果有上一页，就返回上一页
      wx.navigateBack({
        delta: 1,
      });
    } else {
      wx.redirectTo({
        url: '/pages/home/home',
      })
    }
  },
  //退款订单
  refund() {
    let that = this;
    wx.showModal({
      content:
        "退款后消费金额原路退回,\r\n 是否确认退款？",
      confirmColor: "#1D2A73",
      cancelColor: "#666",
      confirmText: "确定",
      success(res) {
        if (res.confirm) {
          let data = {
            order_id: that.data.orderInfoData.order_id,
          };
          request("order/refund", { data }).then((res) => {
            if (res.errcode == 1) {
              wx.showToast({
                title: "退款成功",
              });
              that.getDetail();
            }
          });
        } else if (res.cancel) {
          console.log("用户点击取消");
        }
      },
    });
  },
  // 再来一单
  repurchase() {
    let data = {
      order_id: this.data.orderInfoData.order_id,
    };
    request("integral/againorder", { data }).then((res) => {
      if (res.errcode == 1) {
        wx.showToast({
          title: res.message,
        });
        setTimeout(() => {
          wx.redirectTo({
            url: `/pages/order/detail?order_id=${res.data.order_id}`,
          });
        }, 1000);
      }
    });
  },
  onLoad(options) {
    let { order_id, admins } = options;
    // admins 判断是否是商家扫码进入
    this.setData({
      order_id,
      admins,
    });
    this.getDetail();
    this.data.times1 = setInterval(() => {
          this.getDetail()
        }, 3000);
  },

  onReady() {},

  onHide(){
    clearInterval(this.data.times1)
    this.setData({
      times1:null
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.data.times1)
    this.setData({
      times1:null
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
